package com.sright.bcard.user.biz.manager.impl;

import com.sright.bcard.common.service.share.dto.response.ConnnectUserListResponse;
import com.sright.bcard.common.service.share.dto.user.CardConnectUserInfoDTO;
import com.sright.bcard.common.service.share.exception.base.BusinessErrorException;
import com.sright.bcard.common.service.share.exception.code.BusinessCode;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.content.dal.dao.CardDAO;
import com.sright.bcard.content.dal.entity.CardDO;
import com.sright.bcard.content.dal.result.CardListDTO;
import com.sright.bcard.user.biz.manager.CardConnectUserInfoManager;
import com.sright.bcard.user.dal.dao.CardConnectUserInfoDAO;
import com.sright.bcard.user.dal.entity.CardConnectUserInfoDO;
import com.sright.bcard.user.dal.entity.param.ConnnectUserListParam;
import com.sright.bcard.user.dal.entity.result.ConnnectUserListDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class CardConnectUserInfoManagerImpl implements CardConnectUserInfoManager {

    @Autowired
    private CardConnectUserInfoDAO cardConnectUserInfoDAO;

    @Autowired
    private CardDAO cardDAO;

    @Override
    public Long addCardConnectUserInfo(CardConnectUserInfoDTO cardConnectUserInfoDTO) {
        if(cardConnectUserInfoDTO.getConnectUserId() == null || cardConnectUserInfoDTO.getConnectUserId() == 0) {
            throw new BusinessErrorException(BusinessCode.NULL_OF_CONNECTUSERID);
        }
        // DTO 转 DO
        if(cardConnectUserInfoDTO != null) {
            CardConnectUserInfoDO cardConnectUserInfoDO = new CardConnectUserInfoDO();
            DataConvertUtils.convertDTO2DO(cardConnectUserInfoDTO, cardConnectUserInfoDO);
            return cardConnectUserInfoDAO.insertSelective(cardConnectUserInfoDO);
        }
        return 0L;
    }

    @Override
    public List<ConnnectUserListResponse> connectUserList(CardConnectUserInfoDTO cardConnectUserInfoDTO) {
        // 查询用户DO列表
        // DTO转化为查询DO
        ConnnectUserListParam connnectUserListParam = new ConnnectUserListParam();
        connnectUserListParam.setName(cardConnectUserInfoDTO.getName());
        connnectUserListParam.setEnterpriseId(cardConnectUserInfoDTO.getEnterpriseId());
        List<ConnnectUserListDO> connnectUserListDOS = cardConnectUserInfoDAO.connectUserList(connnectUserListParam);

        //获取当前企业下的cufaceID
        List<CardListDTO> cardDOS = cardDAO.selectIdListByEnterpriseId(connnectUserListParam.getEnterpriseId());
        if (CollectionUtils.isEmpty(cardDOS)) {
            throw new BusinessErrorException(BusinessCode.NUll_OF_ENTERPRISECARDID);
        }
        Long cardId = cardDOS.get(0).getId();
        // 封装cardId
        if(CollectionUtils.isNotEmpty(connnectUserListDOS)) {
//            List<Long> cardBaseInfoIds = connnectUserListDOS.stream().map
//                    (ConnnectUserListDO::getCardBaseInfoId).collect(Collectors.toList());
//            List<CardDO> cardDOS = cardDAO.selectByCardBaseInfoIds(cardBaseInfoIds);
//            if(CollectionUtils.isNotEmpty(cardDOS)) {
//                Map<Long, CardDO> longCardDOMap = cardDOS.stream().collect
//                        (Collectors.toMap(CardDO::getBaseInfoId, a -> a, (k1, k2) -> k1));
//                connnectUserListDOS.stream().forEach(connnectUserListDO -> {
//                    CardDO cardDO = longCardDOMap.get(connnectUserListDO.getCardBaseInfoId());
//                    if(null != cardDO) {
//                        connnectUserListDO.setCardId(cardDO.getId());
//                    }
//                });
//            }
            for (ConnnectUserListDO connnectUserListDO : connnectUserListDOS) {
                connnectUserListDO.setCardId(cardId);
            }
        }
        // DO 转 DTO
        List<ConnnectUserListResponse> connnectUserListResponses = new ArrayList<>();
        DataConvertUtils.convertDOList2DTOList(connnectUserListDOS, connnectUserListResponses, ConnnectUserListResponse.class);
        return connnectUserListResponses;
    }
}
